/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.listener;

import cn.hutool.crypto.digest.BCrypt;
import cn.hutool.json.JSONUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.anwen.mongo.conditions.query.LambdaQueryChainWrapper;
import com.comac.ins.common.core.utils.SpringUtils;
import com.comac.ins.common.core.utils.StringUtils;
import com.comac.ins.common.excel.core.ExcelListener;
import com.comac.ins.common.excel.core.ExcelResult;
import com.comac.ins.common.satoken.utils.LoginHelper;
import com.comac.ins.isfd.domain.IsfdOperator;
import com.comac.ins.isfd.domain.bo.IsfdOperatorParameterConfigBo;
import com.comac.ins.isfd.domain.vo.IsfdOperatorParameterConfigImportVo;
import com.comac.ins.isfd.domain.vo.IsfdOperatorParameterConfigVo;
import com.comac.ins.isfd.service.IIsfdOperatorService;
import com.comac.ins.isfd.service.IIsfdParameterConfigService;
import com.comac.ins.system.service.ISysConfigService;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * 算子参数导入数据监听
 *
 * @author hxloongs
 * @date 2024-7-10
 */
@Slf4j
public class InportOperatorParameterConfigListener extends AnalysisEventListener<IsfdOperatorParameterConfigImportVo> implements ExcelListener<IsfdOperatorParameterConfigImportVo> {

    private final IIsfdParameterConfigService iIsfdParameterConfigService;

    private final IIsfdOperatorService iIsfdOperatorService;

    private final Long operUserId;

    private int successNum = 0;
    private int failureNum = 0;
    private final StringBuilder successMsg = new StringBuilder();
    private final StringBuilder failureMsg = new StringBuilder();

    public InportOperatorParameterConfigListener() {
        this.iIsfdParameterConfigService = SpringUtils.getBean(IIsfdParameterConfigService.class);
        this.iIsfdOperatorService = SpringUtils.getBean(IIsfdOperatorService.class);
        this.operUserId = LoginHelper.getUserId();
    }

    @Override
    public void invoke(IsfdOperatorParameterConfigImportVo importVo, AnalysisContext context) {
        // 处理业务逻辑
        // 校验算子是否存在
        String operatorName = importVo.getOperatorName();
        LambdaQueryChainWrapper<IsfdOperator> lambdaQueryChainWrapper = iIsfdOperatorService.lambdaQuery();
        lambdaQueryChainWrapper.eq(IsfdOperator::getOperatorName, operatorName);
        IsfdOperator isfdOperator = iIsfdOperatorService.one(lambdaQueryChainWrapper);
        if (isfdOperator == null) {
            failureNum++;
            failureMsg.append("<br/>").append("算子名为： ").append(operatorName).append(" 不存在，请检查数据，并重新导入失败数据！");
            return;
        }

        String operatorRefId = isfdOperator.get_id();
        // 幂等性操作
        IsfdOperatorParameterConfigVo isfdOperatorParameterConfigVo = iIsfdParameterConfigService.one(operatorRefId, importVo.getPath(),
            importVo.getType(), importVo.getFieldId(), importVo.getFieldType());
        if (isfdOperatorParameterConfigVo != null) {
            failureNum++;
            failureMsg.append("<br/>").append("第").append(context.getCurrentRowNum()).append("行数据已经存在，不可重复写入，请修改后重试！");
            return;
        }
        IsfdOperatorParameterConfigBo isfdOperatorParameterConfigBo = new IsfdOperatorParameterConfigBo();
        isfdOperatorParameterConfigBo.setOperatorRefId(operatorRefId);
        isfdOperatorParameterConfigBo.setOperatorName(operatorName);
        isfdOperatorParameterConfigBo.setType(importVo.getType());
        isfdOperatorParameterConfigBo.setPath(importVo.getPath());
        isfdOperatorParameterConfigBo.setSortOrder(importVo.getSortOrder());
        isfdOperatorParameterConfigBo.setComponent(importVo.getComponent());
        isfdOperatorParameterConfigBo.setLabel(importVo.getLabel());
        isfdOperatorParameterConfigBo.setFieldId(importVo.getFieldId());
        isfdOperatorParameterConfigBo.setFieldType(importVo.getFieldType());
        String customJsonField = importVo.getCustomJsonField();
        if (StringUtils.isNotBlank(customJsonField)) {
            isfdOperatorParameterConfigBo.setCustomJsonFieldObject(JSONUtil.parseObj(customJsonField));
        }
        iIsfdParameterConfigService.parameterConfigOperatorAdd(isfdOperatorParameterConfigBo);
        successNum++;
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }

    @Override
    public ExcelResult<IsfdOperatorParameterConfigImportVo> getExcelResult() {
        return new ExcelResult<>() {
            @Override
            public String getAnalysis() {
                if (failureNum > 0) {
                    failureMsg.insert(0, "很抱歉，导入部分失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
                    return failureMsg.toString();
                } else {
                    successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
                }
                return successMsg.toString();
            }

            @Override
            public List<IsfdOperatorParameterConfigImportVo> getList() {
                return new ArrayList<>();
            }

            @Override
            public List<String> getErrorList() {
                return null;
            }
        };
    }
}
